package com.fancy.learncenter.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.adapter.base.CustomViewHold;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.DataCategoryDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MoreActivity extends BaseActivity {

    @Bind(R.id.recycleView)
    RecyclerView recycleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("更多");
        initView();
        getDatas();
    }

    CommonRecycleViewAdapter<DataCategoryDataBean> adapter;

    private void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        adapter = new CommonRecycleViewAdapter<DataCategoryDataBean>(this, R.layout.pupopwinodw_data, new ArrayList<DataCategoryDataBean>()) {
            @Override
            public void bindView(CustomViewHold customViewHold, final DataCategoryDataBean item, int position) {


                TextView title = customViewHold.getView(R.id.title);
                title.setText(item.getCategory_name());
                title.setTextColor(Color.parseColor("#000000"));

                if (item.getList() != null) {
                    RecyclerView recyclerView = customViewHold.getView(R.id.recycleView);
                    GridLayoutManager linearLayoutManager = new GridLayoutManager(mContext, 4);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(linearLayoutManager);

                    recyclerView.setAdapter(new CommonRecycleViewAdapter<DataCategoryDataBean.ListBean>(mContext, R.layout.pupopwinodw_data_item, (ArrayList<DataCategoryDataBean.ListBean>) item.getList()) {
                        @Override
                        public void bindView(CustomViewHold customViewHold, final DataCategoryDataBean.ListBean items, int position) {

                            SimpleDraweeView simpleDraweeView = customViewHold.getView(R.id.simpleDraweeView);
                            simpleDraweeView.setImageURI(items.getCategory_icon());


                            TextView title = customViewHold.getView(R.id.title);
                            title.setText(items.getCategory_name());
                            title.setTextColor(Color.parseColor("#000000"));


                            customViewHold.getRoorView().setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(MoreActivity.this, MaterialDataActivity.class);
                                    intent.putExtra("category_id", items.getId() + "");
                                    startActivity(intent);
                                }
                            });

                        }
                    });

                }


            }
        };
        recycleView.setAdapter(adapter);
    }

    @OnClick(R.id.advice)
    public void onViewClicked() {
        Intent intent = new Intent(this, AdviceActivity.class);
        startActivity(intent);
    }

    private void getDatas() {
        Map map = new HashMap();
        HttpMehtod.getInstance().dataCategory(map, new IdeaObserver<BaseDataBean<List<DataCategoryDataBean>>>() {

            @Override
            public void onSuccess(BaseDataBean<List<DataCategoryDataBean>> listBaseDataBean) {

                if (listBaseDataBean.getData() != null) {
                    adapter.notifyDataSetChanged((ArrayList<DataCategoryDataBean>) listBaseDataBean.getData());
                }

            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }
}
